package com.unbox.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import com.unbox.admin.entity.File;
import com.unbox.admin.mapper.FileMapper;
import com.unbox.admin.service.IFileService;
import com.unbox.common.table.UnBoxPage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * <文件表业务实现类
 * </p>
 *
 * @author 王振宇
 * @since 2021-04-20
 */
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, File> implements IFileService {

    @Override
    public File get(File file) {
        LambdaQueryWrapper<File> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(file.getFileId())) {
            lambdaQueryWrapper.eq(File::getFileId, file.getFileId());
        }
        if (!ObjectUtils.isEmpty(file.getCreateTime())) {
            lambdaQueryWrapper.eq(File::getCreateTime, file.getCreateTime());
        }
        if (!ObjectUtils.isEmpty(file.getIsDel())) {
            lambdaQueryWrapper.eq(File::getIsDel, file.getIsDel());
        }
        if (!StringUtils.isEmpty(file.getSaveSource())) {
            lambdaQueryWrapper.eq(File::getSaveSource, file.getSaveSource());
        }
        if (!StringUtils.isEmpty(file.getUrl())) {
            lambdaQueryWrapper.eq(File::getUrl, file.getUrl());
        }
        if (!StringUtils.isEmpty(file.getName())) {
            lambdaQueryWrapper.eq(File::getName, file.getName());
        }
        if (!StringUtils.isEmpty(file.getSuffix())) {
            lambdaQueryWrapper.eq(File::getSuffix, file.getSuffix());
        }
        if (!StringUtils.isEmpty(file.getType())) {
            lambdaQueryWrapper.eq(File::getType, file.getType());
        }
        if (!StringUtils.isEmpty(file.getSource())) {
            lambdaQueryWrapper.eq(File::getSource, file.getSource());
        }
       return super.getOne(lambdaQueryWrapper);
    }

    @Override
    public List<File> getList(File file) {
        LambdaQueryWrapper<File> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(file.getFileId())) {
            lambdaQueryWrapper.eq(File::getFileId, file.getFileId());
        }
        if (!ObjectUtils.isEmpty(file.getCreateTime())) {
            lambdaQueryWrapper.eq(File::getCreateTime, file.getCreateTime());
        }
        lambdaQueryWrapper.eq(File::getIsDel, 0);
        if (!StringUtils.isEmpty(file.getSaveSource())) {
            lambdaQueryWrapper.eq(File::getSaveSource, file.getSaveSource());
        }
        if (!StringUtils.isEmpty(file.getUrl())) {
            lambdaQueryWrapper.eq(File::getUrl, file.getUrl());
        }
        if (!StringUtils.isEmpty(file.getName())) {
            lambdaQueryWrapper.like(File::getName, file.getName());
        }
        if (!StringUtils.isEmpty(file.getSuffix())) {
            lambdaQueryWrapper.eq(File::getSuffix, file.getSuffix());
        }
        if (!StringUtils.isEmpty(file.getType())) {
            lambdaQueryWrapper.eq(File::getType, file.getType());
        }
        if (!StringUtils.isEmpty(file.getSource())) {
            lambdaQueryWrapper.eq(File::getSource, file.getSource());
        }
        return super.list(lambdaQueryWrapper);
    }

    @Override
    public UnBoxPage<List<File>> getPageList(File file, int pageNo, int pageSize) {
        LambdaQueryWrapper<File> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(file.getFileId())) {
            lambdaQueryWrapper.eq(File::getFileId, file.getFileId());
        }
        if (!ObjectUtils.isEmpty(file.getCreateTime())) {
            lambdaQueryWrapper.eq(File::getCreateTime, file.getCreateTime());
        }
        lambdaQueryWrapper.eq(File::getIsDel, 0);
        if (!StringUtils.isEmpty(file.getSaveSource())) {
            lambdaQueryWrapper.eq(File::getSaveSource, file.getSaveSource());
        }
        if (!StringUtils.isEmpty(file.getUrl())) {
            lambdaQueryWrapper.eq(File::getUrl, file.getUrl());
        }
        if (!StringUtils.isEmpty(file.getName())) {
            lambdaQueryWrapper.like(File::getName, file.getName());
        }
        if (!StringUtils.isEmpty(file.getSuffix())) {
            lambdaQueryWrapper.eq(File::getSuffix, file.getSuffix());
        }
        if (!StringUtils.isEmpty(file.getType())) {
            lambdaQueryWrapper.eq(File::getType, file.getType());
        }
        if (!StringUtils.isEmpty(file.getSource())) {
            lambdaQueryWrapper.eq(File::getSource, file.getSource());
        }
        lambdaQueryWrapper.orderByDesc(File::getCreateTime);
        Page<File> page = new Page<>(pageNo,pageSize);
        IPage<File> pageResult = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        UnBoxPage<List<File>> unBoxPage = new UnBoxPage<>();
        unBoxPage.setPageNo(pageNo);
        unBoxPage.setPageSize(pageSize);
        unBoxPage.setSumPageCount(pageResult.getPages());
        unBoxPage.setSumDataCount(pageResult.getTotal());
        unBoxPage.setDatas(pageResult.getRecords());
        return unBoxPage;
    }

    @Override
    public List<File> getIdsList(List<Integer> ids) {
        LambdaQueryWrapper<File> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(File::getFileId, ids);
        return super.list(lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean save(File entity) {
        return super.save(entity);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean saveBatch(List<File> entityList) {
        return super.saveBatch(entityList);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean update(File file) {
        LambdaQueryWrapper<File> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(File::getFileId, file.getFileId());
        return super.update(file, lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateBatchById(List<File> entityList) {
        return super.updateBatchById(entityList);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean remove(File file) {
        LambdaQueryWrapper<File> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(File::getFileId, file.getFileId());
        file.setIsDel(1);
        return update(file,lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean removeByIds(List<Integer> idList) {
        List<File> files = idList.stream().map(str -> {
            File file = new File();
            file.setIsDel(1);
            file.setFileId(str);
            return file;
        }).collect(Collectors.toList());
        return super.updateBatchById(files);
    }
}
